package com.h3c.ptability.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.custom.BusinessException;
import com.eos.common.util.Result;
import com.google.common.base.Charsets;
import com.h3c.ptability.dto.ExcelFileDTO;
import com.h3c.ptability.entity.PtDept;
import com.h3c.ptability.entity.PtEnum;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.IPtEnumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenyifan
 * @date 2022/4/29 10:47
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/excel")
public class ExcelFileController {

    @Resource
    private IPtEnumService ptEnumService;

    @Resource
    private CommService commService;

    @ApiOperation(value = "用于excel模板导出", notes = "用于excel模板导出")
    @PostMapping(value = "/getExcelFile")
    public Map<String, String> getExcelFile(@Valid @RequestBody ExcelFileDTO excelFileDTO) {
        try {
            File downLoadFile = ptEnumService.getExcelFile(excelFileDTO);
            FileInputStream inputStream = new FileInputStream(downLoadFile);
            byte[] buffer = new byte[(int) downLoadFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Map<String, String> result = new HashMap<>(4);
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", downLoadFile.getName());
            FileUtil.del(downLoadFile);
            return result;
        } catch (Exception e) {
            log.error("导出excel报错,原因:", e);
            throw new BusinessException(StatusCodeEnum.REQ_DOWNLOAD_FAIL);
        }
    }

    /**
     * 导出包含两个sheet页的文件
     * @param excelFileDTO
     * @return
     */
    @PostMapping(value = "/downloadExcelFile")
    public Map<String, String> downloadExcelFile(@Valid @RequestBody ExcelFileDTO excelFileDTO) {
        try {
            File downLoadFile = commService.generateExportFile(excelFileDTO.getFileName(), excelFileDTO.getTemplateCode(), excelFileDTO.getTemplateCode2(),
                                            excelFileDTO.getAppId(), excelFileDTO.getDataList(), excelFileDTO.getDataList2());
            FileInputStream inputStream = new FileInputStream(downLoadFile);
            byte[] buffer = new byte[(int) downLoadFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Map<String, String> result = new HashMap<>(4);
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", downLoadFile.getName());
            FileUtil.del(downLoadFile);
            return result;
        } catch (Exception e) {
            log.error("导出excel报错,原因:{}",e.getMessage());
            throw new BusinessException(StatusCodeEnum.REQ_DOWNLOAD_FAIL);
        }
    }
}
